 import { TEXT } from "./const";

//创建react element
function createElement(type, config, ...children) {
    if(config) {
        delete config._source
        delete config._self
    }
    const props = {
        ...config,
        //此处jsx中有两种节点,元素节点和文本节点
        children: children.map(child => typeof child === 'object' ? child : createTextNode(child))
    };
    delete props.key;
    return {
        key: config.key || "",
        type,
        props,
    };
}; 
//把文本节点的样式改造成与元素节点的样式一致,方便遍历
function createTextNode(text) {
    return {
        // key: "",
        type: TEXT,
        props: {
            children: [],
            nodeValue: text,
        }
    }
}

export default {
    createElement,
    createTextNode
};
